﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using iTunesLib;

namespace Jarvis.Sensors
{
    class iTunesSensor : Sensor
    {
        public iTunesSensor()
            : base(1000)
        {
            this.name = "iTunes Sensor";
        }

        public override void Sense(Object stateInfo)
        {
            try
            {
                if (!ProcessSensor.IsRunning("iTunes"))
                {
                    Sensor track_name = new Sensor("iTunes Track");
                    track_name.log(null);

                    Sensor artist = new Sensor("iTunes Artist");
                    artist.log(null);

                    Sensor playlist = new Sensor("iTunes Playlist");
                    playlist.log(null);

                    Sensor genre = new Sensor("iTunes Genre");
                    genre.log(null);

                    Sensor album = new Sensor("iTunes Album");
                    album.log(null);

                    Sensor state = new Sensor("iTunes State");
                    state.log(null);

                    return;
                }

                iTunesAppClass itunes = new iTunesAppClass();

                IITTrack current = itunes.CurrentTrack;

                if (current != null)
                {
                    Sensor track_name = new Sensor("iTunes Track");
                    track_name.log(current.Name);

                    Sensor artist = new Sensor("iTunes Artist");
                    artist.log(current.Artist);

                    Sensor playlist = new Sensor("iTunes Playlist");
                    playlist.log(itunes.CurrentPlaylist.Name);

                    Sensor genre = new Sensor("iTunes Genre");
                    genre.log(current.Genre);

                    Sensor album = new Sensor("iTunes Album");
                    album.log(current.Album);

                    Sensor state = new Sensor("iTunes State");
                    if (itunes.PlayerState == ITPlayerState.ITPlayerStatePlaying)
                        state.log("Playing");
                    else if (itunes.PlayerState == ITPlayerState.ITPlayerStateStopped)
                        state.log("Stopped");
                    else
                        state.log(itunes.PlayerState);
                }
                else
                {
                    Sensor track_name = new Sensor("iTunes Track");
                    track_name.log(null);

                    Sensor artist = new Sensor("iTunes Artist");
                    artist.log(null);

                    Sensor playlist = new Sensor("iTunes Playlist");
                    playlist.log(null);

                    Sensor genre = new Sensor("iTunes Genre");
                    genre.log(null);

                    Sensor album = new Sensor("iTunes Album");
                    album.log(null);

                    Sensor state = new Sensor("iTunes State");
                    state.log(null);
                }
            }
            catch(Exception)
            {

            }
        }
    }
}
